package com.distribute.application.api.utils.mobile;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.commons.lang3.RandomStringUtils;

import com.distribute.framework.plugin.Cache;


public class MobileCodeUtil {
    private static Integer captchaTime = 15 * 60; // 15 minutes
    /**
	 * 短信验证码线程池
	 */
	private static ExecutorService threadPool = Executors.newCachedThreadPool();
    public static String genCaptcha(String phone) {
    	Cache.deleteValue(MobileCodeUtil.class, phone);
        String captcha = RandomStringUtils.randomNumeric(4);
        Cache.setValue(MobileCodeUtil.class, phone, captcha, captchaTime);
        return captcha;
    }
/**
 * 获取redis 中存过的验证码
 */
    public static Boolean validateCaptcha(String phone, String captcha) {
        if (Cache.getValue(MobileCodeUtil.class, phone) == null
                || !Cache.getValue(MobileCodeUtil.class, phone).equals(captcha)) {
            return false;
        } else {
        	Cache.deleteValue(MobileCodeUtil.class, phone);
            return true;
        }
    }
    public static void sendTextMessage(String phone, String captcha) {
    	TextMessageRunnable textMessageRunable = new TextMessageRunnable();
		textMessageRunable.setPhoneNum(phone);
		textMessageRunable.setMsg("【送啊优品】您的验证码是" + captcha + "。如非本人操作，请忽略本短信");
		threadPool.execute(textMessageRunable);
		//(new Thread(textMessageRunable)).start();
    }
   /**
    * 配送商接单提醒
    * @param phone
    * @param count
    */
    public static void sendTextDistributor(String phone, Long count) {
    	TextMessageRunnable textMessageRunable = new TextMessageRunnable();
		textMessageRunable.setPhoneNum(phone);
		textMessageRunable.setMsg("【送啊优品】您有" + count + "条待接单订单，请及时登录小程序配送商端查看。");
		threadPool.execute(textMessageRunable);
		//(new Thread(textMessageRunable)).start();
    }
     /**
      * 合伙人购买
      * @param phone
      * @param address
      * @param time
      */
    public static void sendTextHhr(String phone,String address, String time) {
    	TextMessageRunnable textMessageRunable = new TextMessageRunnable();
		textMessageRunable.setPhoneNum(phone);
		textMessageRunable.setMsg("【送啊优品】您已成功购买" + address + "合伙人，有效期至："+time+"。");
		threadPool.execute(textMessageRunable);
		//(new Thread(textMessageRunable)).start();
    }
    /**
     * 配送商接单发货
     * @param phone
     * @param no
     * @param name
     */
    public static void sendTextPsJieDan(String phone, String no,String name) {
    	TextMessageRunnable textMessageRunable = new TextMessageRunnable();
		textMessageRunable.setPhoneNum(phone);
		textMessageRunable.setMsg("【送啊优品】您的订单：" + no + "，由配送商："+name+"配送，请注意查收。");
		threadPool.execute(textMessageRunable);
		//(new Thread(textMessageRunable)).start();
    }
    /**
     * 品牌发货：
     * @param phone
     * @param no
     * @param kdno
     */
    public static void sendTextPPFH(String phone, String no, String kdno) {
    	TextMessageRunnable textMessageRunable = new TextMessageRunnable();
		textMessageRunable.setPhoneNum(phone);
		textMessageRunable.setMsg("【送啊优品】您的订单：" + no + "，由品牌发货，快递单号："+kdno+"，请注意查看物流信息，您也可以在小程序内查看订单的物流信息。");
		threadPool.execute(textMessageRunable);
		//(new Thread(textMessageRunable)).start();
    }
    /**
     * 配送商注册审核通过
     * @param phone
     */
    public static void sendTextPsShenHeTG(String phone) {
    	TextMessageRunnable textMessageRunable = new TextMessageRunnable();
		textMessageRunable.setPhoneNum(phone);
		textMessageRunable.setMsg("【送啊优品】您的配送商注册申请通过，账号：" + phone + "。");
		threadPool.execute(textMessageRunable);
		//(new Thread(textMessageRunable)).start();
    }
    /**
     * 配送商注册审核失败
     * @param phone
     * @param liyou
     */
    public static void sendTextPsShenHeBTG(String phone, String liyou) {
    	TextMessageRunnable textMessageRunable = new TextMessageRunnable();
		textMessageRunable.setPhoneNum(phone);
		textMessageRunable.setMsg("【送啊优品】您的配送商注册申请没有通过，拒绝理由：" + liyou + "。");
		threadPool.execute(textMessageRunable);
		//(new Thread(textMessageRunable)).start();
    }
    
    /**
     * 配送商提现通过
     * @param phone
     */
    public static void sendTextPsTxTG(String phone) {
    	TextMessageRunnable textMessageRunable = new TextMessageRunnable();
		textMessageRunable.setPhoneNum(phone);
		textMessageRunable.setMsg("【送啊优品】您的提现申请已通过，请注意查看打款信息。");
		threadPool.execute(textMessageRunable);
		//(new Thread(textMessageRunable)).start();
    }
    /**
     * 配送商提现不通过
     * @param phone
     * @param liyou
     */
    public static void sendTextPsTxBTG(String phone, String liyou) {
    	TextMessageRunnable textMessageRunable = new TextMessageRunnable();
		textMessageRunable.setPhoneNum(phone);
		textMessageRunable.setMsg("【送啊优品】您的提现申请没有通过，拒绝理由：" + liyou + "。");
		threadPool.execute(textMessageRunable);
		//(new Thread(textMessageRunable)).start();
    }
    
    /**
     * 品牌商提现通过
     * @param phone
     */
    public static void sendTextPingPaiTxTG(String phone) {
    	TextMessageRunnable textMessageRunable = new TextMessageRunnable();
		textMessageRunable.setPhoneNum(phone);
		textMessageRunable.setMsg("【送啊优品】您的提现申请已通过，请注意查看打款信息。");
		threadPool.execute(textMessageRunable);
		//(new Thread(textMessageRunable)).start();
    }
    /**
     * 品牌商提现不通过
     * @param phone
     * @param liyou
     */
    public static void sendTextPingPaiTxBTG(String phone, String liyou) {
    	TextMessageRunnable textMessageRunable = new TextMessageRunnable();
		textMessageRunable.setPhoneNum(phone);
		textMessageRunable.setMsg("【送啊优品】您的提现申请没有通过，拒绝理由：" + liyou + "。");
		threadPool.execute(textMessageRunable);
		//(new Thread(textMessageRunable)).start();
    }
    
    
    public static void sendTextHxDd(String phone, String liyou) {
    	TextMessageRunnable textMessageRunable = new TextMessageRunnable();
		textMessageRunable.setPhoneNum(phone);
		textMessageRunable.setMsg("【送啊优品】您的配送商注册申请没有通过，拒绝理由：" + liyou + "。");
		threadPool.execute(textMessageRunable);
		//(new Thread(textMessageRunable)).start();
    }
}
